//引入接口
import { reqCategoryList, reqGetBannerList, reqFloorList } from '@/api'

//home模块小仓库
const state = {
    // 服务器返回的是一个数组
    categoryList: [],
    //轮播图数据
    bannerList: [],
    //floor组件数据
    floorList: []
}
const mutations = {
    CATEGORYLIST(state, categoryList) {
        state.categoryList = categoryList;
    },
    GETBANNERLIST(state, bannerList) {
        state.bannerList = bannerList;
    },
    GETFLOORLIST(state, floorList) {
        state.floorList = floorList;
    }
}
const actions = {
    //通过API接口函数调用,向服务器发请求,获取服务器数据
    async categoryList({ commit }) {
        //向服务器请求数据
        let result = await reqCategoryList();
        if (result.code == 200) {
            commit("CATEGORYLIST", result.data)
        }
    },

    //获取首页轮播图
    async getBannerList({ commit, state, dispatch }) {
        //获取首页数据
        let result = await reqGetBannerList();
        //如果获取轮播图成功,提交给mutation，渲染
        if (result.code == 200) {
            commit('GETBANNERLIST', result.data)
        }
    },

    // 获取floor数据
    //等待mock数据返回
    async getFloorList({ commit }) {
        let result = await reqFloorList();
        //如果成功获取(返回值==200,就提交给mutation存储到仓库)
        if (result.code == 200) {
            commit("GETFLOORLIST", result.data)
        }
    }
}
// 计算属性
const getters = {}

export default {
    state,
    mutations,
    actions,
    getters
}